package com.yyz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyz.domain.Category;
import com.yyz.domain.Dish;
import com.yyz.domain.Setmeal;
import com.yyz.domain.SetmealDish;
import com.yyz.dto.SetmealDto;
import com.yyz.mapper.CategoryMapper;
import com.yyz.mapper.SetMealDishMapper;
import com.yyz.mapper.SetMealMapper;
import com.yyz.service.SetMealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author yyz
 * @create 2023-07-20 22:13
 */
@Service
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealMapper setMealMapper;

    @Autowired
    private SetMealDishMapper setMealDishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 判定该套餐分类是否关联了套餐
     * @param ids
     * @return
     */
    @Override
    public Integer count(Long ids) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getCategoryId,ids);

        Integer SetMealCount = setMealMapper.selectCount(lqw);
        return SetMealCount;
    }

    /**
     * 添加套餐
     * @param setmealDto
     */
    @Override
    public void insert(SetmealDto setmealDto) {
        setMealMapper.insert(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        Long id = setmealDto.getId();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(id);
            setMealDishMapper.insert(setmealDish);
        }
    }

    /**
     * 分页查询套餐
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<SetmealDto> page(Integer page, Integer pageSize, String name) {
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        Page<SetmealDto> pageInfo = new Page<>();

        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(name),Setmeal::getName,name)
                .orderByDesc(Setmeal::getUpdateTime);

        setMealMapper.selectPage(setmealPage, lqw);

        BeanUtils.copyProperties(setmealPage,pageInfo,"records");

        List<Setmeal> records = setmealPage.getRecords();
        ArrayList<SetmealDto> setmealDtos = new ArrayList<>();
        for (Setmeal record : records) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(record,setmealDto);

            Long id = record.getCategoryId();
            Category category = categoryMapper.selectById(id);

            setmealDto.setCategoryName(category.getName());
            setmealDtos.add(setmealDto);

        }
        pageInfo.setRecords(setmealDtos);

        return pageInfo;
    }

    /**
     * 根据id页面回显
     * @param id
     * @return
     */
    @Override
    public SetmealDto selectById(Long id) {
        Setmeal setmeal = setMealMapper.selectById(id);

        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);

        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes = setMealDishMapper.selectList(lqw);

        setmealDto.setSetmealDishes(setmealDishes);

        return setmealDto;
    }

    /**
     * 修改套餐
     * @param setmealDto
     */
    @Override
    @Transactional
    public void update(SetmealDto setmealDto) {
        setMealMapper.updateById(setmealDto);

        Long id = setmealDto.getId();

        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,id);
        setMealDishMapper.delete(lqw);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(id);
            setMealDishMapper.insert(setmealDish);
        }
    }

    /**
     * 删除套餐
     * @param ids
     */
    @Override
    public void delete(Long[] ids) {
        for (Long id : ids) {
            setMealMapper.deleteById(id);
        }
    }

    /**
     * 停售 启售
     * @param statusId
     * @param ids
     */
    @Override
    public void status(Integer statusId, Long[] ids) {
        for (Long id : ids) {
            Setmeal setmeal = setMealMapper.selectById(id);

            setmeal.setStatus(statusId);

            setMealMapper.updateById(setmeal);
        }
    }

    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getCategoryId,setmeal.getCategoryId())
                .eq(Setmeal::getStatus,setmeal.getStatus());

        List<Setmeal> setmeals = setMealMapper.selectList(lqw);
        return setmeals;
    }

}
